package com.suning.sawp.web.controller.in;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.Principal;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import com.google.common.base.Charsets;
import com.suning.sawp.constants.ConfigConstants;
import com.suning.sawp.constants.ErrorCodeConstants;
import com.suning.sawp.constants.LoggerConstants;
import com.suning.sawp.dto.common.QueryResult;
import com.suning.sawp.dto.common.ReturnMsg;
import com.suning.sawp.dto.precharge.CommonRtn;
import com.suning.sawp.dto.promotion.CouponSendRecordBean;
import com.suning.sawp.dto.promotion.HorizProRecordShowDto;
import com.suning.sawp.dto.promotion.QueryHorActBean;
import com.suning.sawp.dto.promotion.StatisticsHorCoupon;
import com.suning.sawp.exception.MobileException;
import com.suning.sawp.intf.promotion.HorizPromotionService;
import com.suning.sawp.intf.promotion.ImportPromotionService;
import com.suning.sawp.intf.promotion.PromotionActivityService;
import com.suning.sawp.po.promotion.HorizProActivity;
import com.suning.sawp.po.promotion.HorizProCoupon;
import com.suning.sawp.service.util.BaseContants;
import com.suning.sawp.service.util.ConfigUtil;
import com.suning.sawp.service.util.DateUtils;
import com.suning.sawp.service.util.RedisCacheUtils;
import com.suning.sawp.web.controller.DjBaseController;
import com.suning.sawp.web.exception.MyGsonView;
/**
 * 
 * 促销券后台业务逻辑<br> 
 * 〈功能详细描述〉
 *
 * @author 12061818
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Controller
@RequestMapping("/coupon")
public class CouponInController extends DjBaseController {
    
    private static final Logger LOGGER = LoggerFactory.getLogger(LoggerConstants.SAWP_SERVICE);
    
    /**
     * 生成券的随机数为6位，故最大值为999999
     */
    private static final int MAX_COUNT = 999999;
    @Autowired
    PromotionActivityService promotionActivityService;
    
    @Autowired
    HorizPromotionService horizPromotionService;
    
    @Autowired
    ImportPromotionService importPromotionService;
    
    /**
     * 缓存工具
     */
    @Autowired
    RedisCacheUtils cacheUtils;
    
    /**
     * 
     * 功能描述: 是否超过最大导出数量<br>
     * 〈功能详细描述〉
     * 
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/checkExportCount.do")
    public MyGsonView isExportBeyond(String startTime, String endTime) {
        MyGsonView gsonView = createGsonView();
        ReturnMsg<CommonRtn> ret = new ReturnMsg<CommonRtn>();
        CommonRtn data = new CommonRtn();
        if (StringUtils.isBlank(startTime) || StringUtils.isBlank(endTime)) {
            ret.setRetFlag(ReturnMsg.FAIL);
            ret.setErrorCode(ErrorCodeConstants.ERR_0001);
            ret.setErrorMessage(ErrorCodeConstants.ERR_MAP.get(ErrorCodeConstants.ERR_0001));
            return gsonView;
        }
        try {
            Integer count = promotionActivityService.queryExportCount(startTime, endTime);
            String maxCount = ConfigUtil.getConfig(ConfigConstants.SAWP_CONFIG_PATH,
                    ConfigConstants.MAX_EXPORT_COUNT, ConfigConstants.DEFAULT_MAX_EXPORT_COUNT);
            if(count != null && count <= Integer.parseInt(maxCount)){
                ret.setRetFlag(ReturnMsg.SUCCESS);
                data.setSysMsg(count.toString());
                ret.setData(data);
            }else if(count != null){
                ret.setError(ErrorCodeConstants.ERR_0006, maxCount);
            }
        } catch (Exception e) {
            LOGGER.error("CouponInController.isExportBeyond error.", e);
            ret.setRetFlag(ReturnMsg.FAIL);
            ret.setErrorCode(ErrorCodeConstants.ERR_0002);
            ret.setErrorMessage(ErrorCodeConstants.ERR_MAP.get(ErrorCodeConstants.ERR_0002));
        }
        gsonView.addStaticAttribute("ret", ret);
        gsonView.setJsonObjectName("ret");
        return gsonView;
    }
    
    @RequestMapping("/exportRecordExcel.do")
    public void exportOrderRecord(String startTime,String endTime,
            HttpServletResponse response){
        InputStream is = null;
        OutputStream os = null;
        File file = null;
        try {
            file = promotionActivityService.exportRecordExcel(startTime, endTime);
            is = new FileInputStream(file);
            os = response.getOutputStream();
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/x-download");
            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
            String time = formatter.format(new Date());
            String fileName = "couponRecord_" + time + ".xlsx";
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            byte[] buffer = new byte[4096];
            int len = 0;
            while ((len = is.read(buffer)) != -1) {
                os.write(buffer, 0, len);
            }
            os.flush();
        } catch (Exception e) {
            LOGGER.error("批量导出出现错误", e);
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    LOGGER.error("关闭os流失败", e);
                }
            }
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    LOGGER.error("关闭is流失败", e);
                }
            }
            if (file != null) {
                try {
                    file.delete();
                } catch (Exception e) {
                    LOGGER.error("删除file：" + file.getAbsolutePath() + "失败", e);
                }
            }
        }
    }
    
    /**
     * 
     * 功能描述: 后台分页查询异业券促销活动列表<br>
     * 〈功能详细描述〉
     *
     * @param activityName
     * @param proActivityId
     * @param startTime
     * @param endTime
     * @param currIndex
     * @param pageSize
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/queryHorizActivity.do")
    public MyGsonView queryHorizActivity(String activityName, String proActivityId, String startTime,
            String endTime, Integer pageNo, Integer pageSize, Principal principal){
        Object[] args = {activityName, proActivityId, startTime, endTime, pageNo, pageSize, principal.getName()};
        LOGGER.debug("CouponInController.queryHorizActivity, activityName:{}, proActivityId:{}, startTime:{}, endTime:{}, pageNo:{}, pageSize:{}, staffId:{}",
                args);
        ReturnMsg<QueryResult<QueryHorActBean>> rtn = new ReturnMsg<QueryResult<QueryHorActBean>>();
        
        // 设置默认的页面大小和起始位置10,0
        if (null == pageSize || pageSize < 1) {
            pageSize = 10;
        }
        if (null == pageNo || pageNo < 1) {
            pageNo = 1;
        }

        try {
            int startIndex = (pageNo - 1) * pageSize;
            rtn = horizPromotionService.queryHorizActivity(activityName, proActivityId, 
                    startTime, endTime, startIndex, pageSize, principal.getName());
        } catch (Exception e) {
            rtn.setError(ErrorCodeConstants.ERR_0002);
           LOGGER.error("查询促销活动失败。", e);
        }
        return setGsonView(rtn);
    }
    
    /**
     * 
     * 功能描述: 查询某店员的批量导入记录<br>
     * 〈功能详细描述〉
     *
     * @param principal 
     * @param startIndex
     * @param limit
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/queryHorizProRecords.do")
    public MyGsonView queryHorizProRecords(String activityId, int pageNo, int pageSize) {
        // 设置默认的页面大小和起始位置10,0
        if (pageSize < 1) {
            pageSize = 10;
        }
        if (pageNo < 1) {
            pageNo = 1;
        }
        int startIndex = (pageNo - 1) * pageSize;
        ReturnMsg<QueryResult<HorizProRecordShowDto>> retMsg = horizPromotionService.queryHorizProRecords(
                activityId, null, startIndex, pageSize);
        
        return setGsonView(retMsg);
    }
    
    /**
     * 
     * 功能描述: 根据导入批次号删除导入优惠券数据<br>
     * 〈功能详细描述〉
     *
     * @param importBatch
     * @param activityId
     * @param principal
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/deleteHorizProCoupon.do")
    public MyGsonView deleteHorizProCoupon(String importBatch, String activityId
            , Principal principal) {
        LOGGER.debug("CouponInController.deleteHorizProCoupon, importBatch:{}, activityId:{}", importBatch, activityId);
        ReturnMsg<String> returnMsg = horizPromotionService
                .delHorizProCouponByBatchCode(importBatch, activityId, principal.getName());
        return setGsonView(returnMsg);
    }
    
    /**
     * 
     * 功能描述: 导入异业券<br>
     * 
     * @param currIndex 当前索引
     * @param pageSize 页面大小
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/importPro.do")
    public MyGsonView importPromotion(Principal principal, String activityName, String proActivityId,
            String activitySecretKey, String proActivityName, String actRemainCount,
            String startTime, String endTime, MultipartFile importData){
        String staffId = principal.getName();
        LOGGER.debug("开始导入异业券,staffId="+staffId+",activityName="+activityName+
                ",proActivityId="+proActivityId+",proActivityName="+proActivityName);
        ReturnMsg<Integer> ret = new ReturnMsg<Integer>();
        try {
            ret = importPromotionService.importPromotion(activityName, proActivityId, activitySecretKey,
                    proActivityName, actRemainCount, startTime, endTime, staffId, importData.getOriginalFilename(), importData.getInputStream());
        } catch (Exception e) {
            ret.setRetFlag(ReturnMsg.FAIL);
            ret.setErrorCode(ErrorCodeConstants.ERR_0002);
            ret.setErrorMessage(ErrorCodeConstants.ERR_MAP.get(ErrorCodeConstants.ERR_0002));
            LOGGER.error("导入异业券失败,staffId="+staffId+",activityName="+activityName+
                    ",proActivityId="+proActivityId+",proActivityName="+proActivityName+",error="+e);
        }
        
        return setGsonView(ret);
    }
    
    /**
     * 
     * 功能描述: 变更启用状态<br>
     * 〈功能详细描述〉
     * 
     * @param activityId
     * @param optCode
     * @param principal
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/updateActivityStatus.do")
    public MyGsonView updateActivityStatus(String activityId, String optCode, Principal principal) {
        LOGGER.debug("CouponInController.updateActivityStatus, activityId:{}, optCode:{}", activityId, optCode);
        ReturnMsg<String> ret = horizPromotionService
                .updateActivityStatus(activityId, optCode, principal.getName());
        return setGsonView(ret);
    }
    
    /**
     * 
     * 功能描述: 检查已验券的记录数是否已经超过约定值<br>
     * 〈功能详细描述〉
     *
     * @param startTime
     * @param endTime
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/checkValidHorizCouponCount.do")
    public MyGsonView checkValidHorizCouponCount(String startTime, String endTime) {
        ReturnMsg<String> ret = horizPromotionService
                .checkHorizProCouponCount(startTime, endTime);
        return setGsonView(ret);
    }
    
    /**
     * 
     * 功能描述: 导出已经验过券的excel<br>
     * 〈功能详细描述〉
     *
     * @param startTime
     * @param endTime
     * @param response
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/exportValidHorizCoupon.do")
    public void exportValidHorizCoupon(String startTime, String endTime,
            HttpServletResponse response) {
        Workbook workbook = horizPromotionService.exportValidHorizCoupon(startTime, endTime);
        response.setContentType("application/x-download");
        String time = DateUtils.getNowFullStr();
        response.setHeader("Content-disposition", "attachment;filename=coupon_" + time + ".xlsx");    
        OutputStream ouputStream = null;
        try {
            ouputStream = response.getOutputStream();
            workbook.write(ouputStream);
            ouputStream.flush();    
            ouputStream.close();
        } catch (IOException e) {
            LOGGER.error("exportValidHorizCoupon 导出excel出错", e);
        }
    }
    
    /**
     * batchCreateDifferentIndustryCertificate
     * @param activityId
     * @param count
     * @return
     */
    @RequestMapping("/batchCreateDiffIndustCert.do")
    public MyGsonView batchCreateDifferentIndustryCertificate(String activityId, Integer count) {
        MyGsonView gsonView = createGsonView();
        //校验参数,不为空且有效
        if (StringUtils.isBlank(activityId)) {
            setErrorMsg(gsonView, ErrorCodeConstants.ERR_0003, "activityId");
        } else if (null == count || count < 0) {
            setErrorMsg(gsonView, ErrorCodeConstants.ERR_0003, "count");
        } else if (count > MAX_COUNT) {
            //6位随机数，所以最大999999
            setErrorMsg(gsonView, ErrorCodeConstants.ERR_0009);
        } else {
            try {
                //执行批量处理
                horizPromotionService.batchCreateDifferentIndustryCertificate(activityId, count);   
            } catch (Exception e) {
                LOGGER.error("batchCreateDifferentIndustryCertificate error, ", e);
                if (e instanceof MobileException) {
                    String errorCode = ((MobileException)e).getErrorCode();
                    setErrorMsg(gsonView, errorCode);
                } else {
                    //其他是系统异常
                    setErrorMsg(gsonView, ErrorCodeConstants.ERR_0002);
                }
            }
        }
        return gsonView;
    }
    
    /**
     * batchCreateDifferentIndustryCertificate
     * @param activityId
     * @param count
     * @return
     */
    @RequestMapping("/retryCreateCouponFile.do")
    public MyGsonView retryCreateCouponFile(String activityId) {
        MyGsonView gsonView = createGsonView();
        //校验参数,不为空且有效
        if (StringUtils.isBlank(activityId)) {
            setErrorMsg(gsonView, ErrorCodeConstants.ERR_0003, "activityId");
        } else {
            try {
                //执行重试处理
                horizPromotionService.retryCreateCouponFile(activityId);   
            } catch (Exception e) {
                LOGGER.error("retryCreateCouponFile error, ", e);
                if (e instanceof MobileException) {
                    String errorCode = ((MobileException)e).getErrorCode();
                    setErrorMsg(gsonView, errorCode);
                } else {
                    //其他是系统异常
                    setErrorMsg(gsonView, ErrorCodeConstants.ERR_0002);
                }
            }
        }
        return gsonView;
    }
    
    /**
     * 清除在生成异业券过程中使用的缓存信息
     * 防止在生成过程中出现异常导致状态变更不准确
     * 最终限制活动再次生成异业券的操作
     * @param activityId
     * @return
     */
    @RequestMapping("/clearActivityCache.do")
    public MyGsonView clearActivityCache(String activityId) {
        MyGsonView gsonView = createGsonView();
        //校验参数,不为空且有效
        if (StringUtils.isBlank(activityId)) {
            setErrorMsg(gsonView, ErrorCodeConstants.ERR_0003, "activityId");
        } else {
            try {
                //执行清除处理
                cacheUtils.clearCacheData(HorizProCoupon.Status.CACHE_NAME, activityId);
                cacheUtils.clearCacheData(HorizProCoupon.Status.CACHE_NAME, activityId + "_data");
                cacheUtils.clearCacheData(HorizProCoupon.Status.CACHE_NAME, activityId + "_couponHead");
            } catch (Exception e) {
                LOGGER.error("retryCreateCouponFile error, ", e);
                //其他是系统异常
                setErrorMsg(gsonView, ErrorCodeConstants.ERR_0002);
            }
        }
        return gsonView;
    }
    /**
     * 功能描述: 查询发券记录分页
     * 〈功能详细描述〉
     * @param startTime
     * @param endTime
     * @param regionCode
     * @param storeCode
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping("/queryCouponRecord.do")
    public MyGsonView queryCouponRecord(String startTime, String endTime, String activityName,
    		String regionCode, String storeCode, int pageNo, int pageSize){
    	Object[] args = {startTime, endTime, activityName, regionCode, storeCode, pageNo, pageSize};
        LOGGER.debug("CouponInController.queryCouponRecord, startTime:{}, endTime:{}, activityName:{}, regionCode:{}, storeCode:{}, pageNo:{}, pageSize:{}",
                args);
        ReturnMsg<QueryResult<CouponSendRecordBean>> rtn = new ReturnMsg<QueryResult<CouponSendRecordBean>>();
        if(StringUtils.isBlank(startTime) || StringUtils.isBlank(endTime) || StringUtils.isBlank(regionCode)){
            rtn.setError(ErrorCodeConstants.ERR_0001);
            return setGsonView(rtn);
        }
    	// 设置默认的页面大小和起始位置10,0
        if (pageSize < 1) {
            pageSize = 10;
        }
        if (pageNo < 1) {
            pageNo = 1;
        }
        
        try {
			int startIndex = (pageNo - 1) * pageSize;
			rtn = promotionActivityService.queryCouponSendRecord(startTime, endTime, regionCode,
					storeCode, activityName, startIndex, pageSize);
		} catch (Exception e) {
			rtn.setError(ErrorCodeConstants.ERR_0002);
	        LOGGER.error("查询发券记录统计异常", e);
		}
    	return setGsonView(rtn);
    }
    
    /**
     * 
     * 功能描述: 后台分页查询异业券促销活动列表<br>
     * 〈功能详细描述〉
     *
     * @param activityName
     * @param proActivityId
     * @param startTime
     * @param endTime
     * @param currIndex
     * @param pageSize
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/queryHorizCoupon.do")
    public MyGsonView queryHorizCoupon(String activityName, String startTime,String endTime,String storeCode,String regionCode,Integer pageNo, Integer pageSize){
        Object[] args = {activityName, startTime, endTime, storeCode,regionCode,pageNo, pageSize};
        LOGGER.debug("CouponInController.queryHorizCoupon, activityName:{}, startTime:{}, endTime:{}, storeCode:{},regionCode:{},pageNo:{}, pageSize:{}",
                args);
        ReturnMsg<QueryResult<StatisticsHorCoupon>> rtn = new ReturnMsg<QueryResult<StatisticsHorCoupon>>();
        if(StringUtils.isBlank(startTime) || StringUtils.isBlank(endTime) || StringUtils.isBlank(regionCode)){
            rtn.setError(ErrorCodeConstants.ERR_0001);
            return setGsonView(rtn);
        }
        // 设置默认的页面大小和起始位置10,0
        if (null == pageSize || pageSize < 1) {
            pageSize = 10;
        }
        if (null == pageNo || pageNo < 1) {
            pageNo = 1;
        }

        try {
            int startIndex = (pageNo - 1) * pageSize;
            rtn = horizPromotionService.statisHorCoupon(activityName, startTime, endTime, regionCode, storeCode,startIndex,pageSize);
        } catch (Exception e) {
            rtn.setError(ErrorCodeConstants.ERR_0002);
           LOGGER.error("查询异业券统计异常", e);
        }
        return setGsonView(rtn);
    }
    /**
     * 根据活动ID查询活动生成券文件地址
     * @param activityId
     * @return
     */
    @RequestMapping("/downCouponFile.do")
    public MyGsonView downCouponFile(String activityId) {
        MyGsonView gsonView = createGsonView();
        //校验参数,不为空且有效
        if (StringUtils.isBlank(activityId)) {
            setErrorMsg(gsonView, ErrorCodeConstants.ERR_0003, "activityId");
        } else {
            try {
                //查询活动详情
                HorizProActivity activity = horizPromotionService.queryHorizProActivityByActivityId(activityId);
                if (null == activity) {
                    setErrorMsg(gsonView, ErrorCodeConstants.ERR_1010);
                } else if(StringUtils.isBlank(activity.getCouponFileUrl())) {
                    setErrorMsg(gsonView, ErrorCodeConstants.ERR_0018);
                } else {
                    gsonView.addStaticAttribute(BaseContants.DATA, activity.getCouponFileUrl());
                }
            } catch (Exception e) {
                LOGGER.error("retryCreateCouponFile error, ", e);
                if (e instanceof MobileException) {
                    String errorCode = ((MobileException)e).getErrorCode();
                    setErrorMsg(gsonView, errorCode);
                } else {
                    //其他是系统异常
                    setErrorMsg(gsonView, ErrorCodeConstants.ERR_0002);
                }
            }
        }
        return gsonView;
    }
    
    /**
     * 
     * 功能描述: 导出已经验过券的excel<br>
     * 〈功能详细描述〉
     *
     * @param startTime
     * @param endTime
     * @param response
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/exportHorCouponStat.do")
    public void exportHorCouponStat(String activityName, String startTime,String endTime,String storeCode,String regionCode,
            HttpServletResponse response) {
        OutputStream ouputStream = null;
        try {
        if(StringUtils.isBlank(startTime) || StringUtils.isBlank(endTime) || StringUtils.isBlank(regionCode)){
            response.setContentType("text/html; charset=UTF-8");
            response.setHeader("Cache-Control", "no-cache");
            ouputStream = response.getOutputStream();
            ouputStream.write(ErrorCodeConstants.ERR_MAP.get(ErrorCodeConstants.ERR_0001).getBytes(Charsets.UTF_8));
        }else{
            Workbook workbook = horizPromotionService.exportHorCouponStat(activityName, startTime, endTime, regionCode, storeCode);
            response.setContentType("application/x-download");
            String time = DateUtils.getNowFullStr();
            response.setHeader("Content-disposition", "attachment;filename=couponStat_" + time + ".xlsx");    
            ouputStream = response.getOutputStream();
            workbook.write(ouputStream);
        }
        ouputStream.flush();    
        ouputStream.close();
        } catch (IOException e) {
            LOGGER.error("exportValidHorizCoupon 导出excel出错", e);
        }
    }
    /**
     * 
     * 功能描述: 检查已验券的记录数是否已经超过约定值<br>
     * 〈功能详细描述〉
     *
     * @param startTime
     * @param endTime
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/checkCouponStatExport.do")
    public MyGsonView checkCouponStatExport(String activityName, String startTime,String endTime,String storeCode,String regionCode) {
        ReturnMsg<String> ret = horizPromotionService
                .checkCouponStatExport(activityName, startTime, endTime, regionCode, storeCode);
        return setGsonView(ret);
    }
    
    /**
     * 
     * 功能描述: 导出已经验过券的excel<br>
     * 〈功能详细描述〉
     *
     * @param startTime
     * @param endTime
     * @param response
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/exportSendCouponStat.do")
    public void exportSendCouponStat(String activityName, String startTime,String endTime,String storeCode,String regionCode,
            HttpServletResponse response) {
        OutputStream ouputStream = null;
        try {
        if(StringUtils.isBlank(startTime) || StringUtils.isBlank(endTime) || StringUtils.isBlank(regionCode)){
            response.setContentType("text/html; charset=UTF-8");
            response.setHeader("Cache-Control", "no-cache");
            ouputStream = response.getOutputStream();
            ouputStream.write(ErrorCodeConstants.ERR_MAP.get(ErrorCodeConstants.ERR_0001).getBytes(Charsets.UTF_8));
        }else{
            Workbook workbook = promotionActivityService.exportSendCouponStat(activityName, startTime, endTime, regionCode, storeCode);
            response.setContentType("application/x-download");
            String time = DateUtils.getNowFullStr();
            response.setHeader("Content-disposition", "attachment;filename=couponStat_" + time + ".xlsx");    
            ouputStream = response.getOutputStream();
            workbook.write(ouputStream);
        }
        ouputStream.flush();    
        ouputStream.close();
        } catch (IOException e) {
            LOGGER.error("exportValidHorizCoupon 导出excel出错", e);
        }
    }
    /**
     * 
     * 功能描述: 检查已验券的记录数是否已经超过约定值<br>
     * 〈功能详细描述〉
     *
     * @param startTime
     * @param endTime
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/checkSendCouponExport.do")
    public MyGsonView exportSendCouponStat(String activityName, String startTime,String endTime,String storeCode,String regionCode) {
        ReturnMsg<String> ret = promotionActivityService.checkSendCouponStatExport(activityName, startTime, endTime, regionCode, storeCode);
        return setGsonView(ret);
    }
    
}
